package org.linlinjava.litemall.admin.task;

import com.google.common.collect.Lists;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.task.Task;
import org.linlinjava.litemall.core.util.BeanUtil;
import org.linlinjava.litemall.core.util.RedisKey;
import org.linlinjava.litemall.core.util.RedisUtils;
import org.linlinjava.litemall.db.domain.LitemallActivity;
import org.linlinjava.litemall.db.domain.LitemallActivityGroupon;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallStatistics;
import org.linlinjava.litemall.db.service.LitemallActivityService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.util.OrderUtil;

import java.util.Collections;
import java.util.List;

public class ActivityExpiredTask extends Task {

    private final Log logger = LogFactory.getLog(ActivityExpiredTask.class);
    private int activityId = -1;

    public ActivityExpiredTask(Integer activityId, long delayInMilliseconds) {
        super("ActivityExpiredTask-" + activityId, delayInMilliseconds);
        this.activityId = activityId;
    }

    @Override
    public void run() {
        logger.info("系统开始处理延时任务---活动过期---" + this.activityId);

        LitemallActivityService activityService = BeanUtil.getBean(LitemallActivityService.class);
        LitemallOrderService orderService = BeanUtil.getBean(LitemallOrderService.class);

        LitemallActivity activity = activityService.queryById(activityId);
        if (activity == null) {
            return;
        }
        if (!activity.getState().equals(LitemallActivity.StateEnum.ISSUING.getCode())) {
            return;
        }

        // 活动到期下线更新
        activity.setState(LitemallActivity.StateEnum.TIME_OUT.getCode());
        activityService.updateActivityById(activity, null);

        // 清空该活动缓存
        List<String> cacheKeys = Lists.newArrayList();
        cacheKeys.add(RedisKey.getActivityKey(String.valueOf(activity.getId()), String.valueOf(LitemallStatistics.BTypeEnum.VISIT.getCode())));
        cacheKeys.add(RedisKey.getActivityKey(String.valueOf(activity.getId()), String.valueOf(LitemallStatistics.BTypeEnum.SING_UP.getCode())));
        cacheKeys.add(RedisKey.getActivityKey(String.valueOf(activity.getId()), String.valueOf(LitemallStatistics.BTypeEnum.SHARE.getCode())));
        cacheKeys.add(RedisKey.getActivityKey(String.valueOf(activity.getId()), String.valueOf(LitemallStatistics.BTypeEnum.GROUPON.getCode())));
        RedisUtils.delPipeLine(cacheKeys);

        List<LitemallActivityGroupon> activityGroupons = activityService.queryActivityGroupon(Collections.singletonList(activityId));

        for (LitemallActivityGroupon activityGroupon : activityGroupons) {
            Byte state = activityGroupon.getState();
            Byte payState = activityGroupon.getPayState();
            LitemallOrder order = orderService.findById(activityGroupon.getOrderId());
            if (OrderUtil.isCreateStatus(order)) {
                activityGroupon.setState(LitemallActivityGroupon.StateEnum.FAIL.getCode());
                activityGroupon.setPayState(LitemallActivityGroupon.PayStateEnum.FAIL.getCode());
                activityGroupon.setUpdatedBy("ActivityExpiredTask");
                activityService.updateActivityGrouponById(activityGroupon);

                order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL);
                orderService.updateWithOptimisticLocker(order);

            } else if (OrderUtil.isFailPay(order)) {
                activityGroupon.setState(LitemallActivityGroupon.StateEnum.FAIL.getCode());
                activityGroupon.setPayState(LitemallActivityGroupon.PayStateEnum.FAIL.getCode());
                activityGroupon.setUpdatedBy("ActivityExpiredTask");
                activityService.updateActivityGrouponById(activityGroupon);

            } else if (OrderUtil.isPayStatus(order)) {
                if (!LitemallActivityGroupon.StateEnum.SUCCESS.getCode().equals(state)) {
                    activityGroupon.setState(LitemallActivityGroupon.StateEnum.SUCCESS.getCode());
                    activityGroupon.setUpdatedBy("ActivityExpiredTask");
                }

                if (!LitemallActivityGroupon.PayStateEnum.SUCCESS.getCode().equals(payState)) {
                    activityGroupon.setPayState(LitemallActivityGroupon.PayStateEnum.SUCCESS.getCode());
                    activityGroupon.setUpdatedBy("ActivityExpiredTask");
                }

                activityService.updateActivityGrouponById(activityGroupon);
            }
        }
        logger.info("系统结束处理延时任务---活动过期---" + this.activityId);
    }
}
